﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KhuyenMaiVang.DAL;
using System.Web.Routing;

namespace KhuyenMaiVang.Models
{
    public class DataProvider
    {
       private KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();
   //     private DuLieuGia db = new DuLieuGia();
        public IEnumerable<Voucher> DealsToDay(int city = -1, int category = -1, int numPerPage = 10)
        {
            var query = from v in db.Vouchers
                        where v.VoucherStatusID.HasValue
                                && v.VoucherStatusID == 1
                                && (city == -1 || v.CityID.HasValue && v.CityID == city)
                                && (category == -1 || v.CategoryID.HasValue && v.CategoryID == category)
                                && v.EndTime.HasValue
                                && v.CreatedTime.HasValue
                                && v.CostPrice.HasValue
                                && v.SellPrice.HasValue

                        orderby v.CostPrice ascending
                        select v;

            return query.Take(numPerPage);
        }

        public IEnumerable<Voucher> DealsRecent(int city = -1, int category = -1)
        {
            var query = from v in db.Vouchers
                        where v.VoucherStatusID.HasValue
                                && v.VoucherStatusID == 1
                                && (city == -1 || v.CityID.HasValue && v.CityID == city)
                                && (category == -1 || (v.CategoryID.HasValue && v.CategoryID == category))
                                && v.EndTime.HasValue
                                && v.CreatedTime.HasValue
                                && v.CostPrice.HasValue
                                && v.SellPrice.HasValue
                        orderby v.CostPrice ascending
                        select v;
            return query;
        }

        public Voucher GetVoucher(int id)
        {
            var voucher = db.Vouchers.SingleOrDefault(v => v.ID == id
                                                            && v.VoucherStatusID == 1
                                                            && v.EndTime.HasValue
                                                            && v.CreatedTime.HasValue
                                                            && v.CostPrice.HasValue
                                                            && v.SellPrice.HasValue);
            return voucher;
        }
    }
}